package net.chathu.airgw.packets.redirect 
{
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author ze
	 */
	public class Sendable implements FR2GWPacket
	{
		protected static var headerSize:int = 4;
		
		private var data:ByteArray;
		
		public function Sendable() 
		{
			data = new ByteArray();
			data.endian = "littleEndian";
		}
		
		protected function getSize(type:*):int
		{
			if (type is int) {
				
				return 4;
			} else if (typeof(type) == "number") {
				// Not handled!
				return -1;
			} else if (typeof(type) == "string") {
				var ba:ByteArray =  new ByteArray();
				ba.writeUTFBytes(type);
				return ba.position;
			} else {
				//Not handled!
				return -1;
			}
		}
		
		protected function writeShort(short:int):void
		{
			data.writeShort(short);
		}
		
		protected function writeInt(integer:int):void {
			data.writeInt(integer);
		}
		
		protected function writeString(string:String):void {
			data.writeUTFBytes(string);
		}
		
		protected function writeByte(byte:int):void
		{
			data.writeByte(byte);
		}
		
		protected function writeBytes(bytes:ByteArray, offset:int = 0, length:int=0 ):void
		{
			data.writeBytes(bytes, offset, length);
		}
		
		public function getData():ByteArray
		{
			var currPos:int = data.position;
			data.position = 0;
			var ret:ByteArray = new ByteArray();
			data.readBytes(ret, 0, data.bytesAvailable);
			data.position = currPos;
			return ret;
		}
		
	}

}